<?php
namespace app\admin\controller;
use app\common\controller\Common;
use app\admin\model\RuleModel;
use app\admin\model\RoleModel;
use app\admin\model\AdminModel;
use app\common\model\Group;
use app\common\model\Admin;
use app\common\model\Shop;
use app\common\model\Rule;
use think\Exception;
use think\Request;
use think\Session;

/**
* 账号管理
*/
class Account extends Common
{
    /**
	* 管理员列表
	*/
	public function adminlist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			return (new AdminModel())->getAdminList(10,$info['page']);
		}
		return $this->fetch();
	}
	/**
	* 删除
	*/
	public function deladmin()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(AdminModel::delAdmin($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 修改
	*/
	public function editadmin()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				 return json(AdminModel::updateAdmin($info));
			 }catch (\Exception $e){
				return json(back(config('status.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$admin = Admin::get($info['id']);
		$this->assign('admin',$admin);
		$this->assign('shop',Shop::select());
		$this->assign('shopid',Session::get('admin.shop_id'));
		$this->assign('group',AdminModel::group($info['id'],$admin['admin_type']));
		return $this->fetch();	
	}
	/**
	* 添加管理员
	*/
	public function addadmin()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(AdminModel::serAdmin($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
			
		}

		$this->assign('shop',Shop::select());
		$this->assign('shopid',Session::get('admin.shop_id'));
		$this->assign('group',(new Group())->groupList(['admin_type'=>0]));
		return $this->fetch();	
	}
	/**
	* 获取当前角色
	*/
	public function getThisGroup()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return (new Group())->groupList(['admin_type'=>$info['admin_type']]);
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**********************************角色管理*************************************/
	/**
	* 角色管理
	*/
	public function rolelist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			$where = [];
			$type = 1;
			if(Session::get('admin.shop_id') == 0)
			{
				$where['admin_type'] = ['in',[0,1]];
			}else{
				$type = 1;
				$where['shop_id'] = Session::get('admin.shop_id');
			}
			
			return (new Group())->getRoleList($where,$type,10,$info['page']);
		}
		return $this->fetch();
	}
	/**
	* 删除
	*/
	public function delrole()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(RoleModel::delRole($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 修改
	*/
	public function editrole()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				 return json(RoleModel::updateRole($info));
			 }catch (\Exception $e){
				return json(back(config('status.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$group = Group::get($info['id']);
		$this->assign('role',$group);
		$this->assign('ruledata',RoleModel::ruledata($group['rules']));
		return $this->fetch();	
	}
	/**
	* 添加角色
	*/
	public function addrole()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(RoleModel::setRole($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
			
		}
		$this->assign('ruledata',RoleModel::ruledata());
		return $this->fetch();
	}
	/**********************************权限规则*************************************/
	/**
	* 权限规则管理
	*/
	public function rulelist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			return RuleModel::getRuleList(10,$info['page']);
		}
		return $this->fetch();
	}
	/**
	* 删除
	*/
	public function delrule()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(RuleModel::delRule($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 修改
	*/
	public function editrule()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				 return json(RuleModel::updateRule($info));
			 }catch (\Exception $e){
				return json(back(config('status.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$this->assign('rule',Rule::get($info['id']));
		$this->assign('rules',RuleModel::ruleList());
		return $this->fetch();	
	}
	/**
	* 添加权限规则
	*/
	public function addrule()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(RuleModel::serRule($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$this->assign('rule',RuleModel::ruleList());
		return $this->fetch();
	}
}
